#coding=utf-8
import time

import yaml
from uiautomator2 import xpath


from page_object.guang_page import GuangPage
from resource.path_info import *

"""
创建直播预告
"""
class PreviewCreatePage(GuangPage):
    """
    创建直播预告
    """
    def create_preview_broadcast(self, d, title, desc, startType):
        print("take_head_image")
        if d.xpath(confirm_select_goods_xpath).exists:
            d.xpath(confirm_select_goods_xpath).click()
        self.take_head_image(d)
        self.input_title_and_introduce(d,title,desc)
        self.preview_other_config(d, startType)
        d.xpath(save_create_btn_xpath).click()
        self.check_preview_create(d)
        d.press("back")

    """
    修改封面
    """
    def take_head_image(self, d):
        print("take_head_image")
        d.xpath(preview_head_image_xpath).click()
        d.xpath(image_source_self_xpath).click()
        time.sleep(1)
        d.xpath(to_take_photo_xpath).click()
        time.sleep(1)
        d.xpath(take_photo_xpath).click()
        d.xpath(pre_confirm_photo_xpath).wait(timeout=3.0)
        d.xpath(pre_confirm_photo_xpath).click()
        d.xpath(confirm_photo_xpath).click()
        return self

    """
    输入标题及介绍
    """
    def input_title_and_introduce(self, d, title, desc):
        print("input_title_and_descp")
        d.xpath(preview_title_xpath).click()
        d(focused=True).send_keys(title)
        d.xpath(preview_introduce_xpath).click()
        d(focused=True).send_keys(desc)
        d.swipe(0.7, 0.4, 0.7, 0.1, 0.13)
        if d.xpath(sougou_input_title_xpath).exists:
            d.press("back")
        return self

        """
        输入其他信息
        """
    def preview_other_config(self, d, startType="startType"):
        print("preview_other_config")
        #select goods
        d.xpath(preview_to_select_goods_xpath).click()
        d.xpath(goodsCheckBox_xpath %1).click()
        d.xpath(confirm_select_goods_xpath).click()
        d.xpath(save_select_goods_xpath).click()
        # select Channel
        d.xpath(preview_channel_xpath).click()
        d.xpath(preview_channel_first_xpath).click()
        # ensure startDate
        d.xpath(to_set_start_date_xpath).click()
        if startType == "current":
            # 滑动分钟坐标
            d.swipe(0.869, 0.906, 0.864, 0.651, 0.5)
        elif startType == "future":
            # 滑动小时坐标
            d.swipe(0.682, 0.907, 0.674, 0.853, 0.13)
        else:
            d.swipe(0.869, 0.906, 0.864, 0.651, 0.5)
        d.xpath(ensure_start_date_xpath).click()
        return self


    """
    输入其他信息
    """

    def check_preview_create(self, d):
        print("check_preview_create")
        d(text=preview_create_success_text).wait(timeout=10.0)
        if d(text=preview_create_success_text).exists:
            print("create successfully")
        else:
            raise Exception("create fail")
        return self

    def copy_preview_create(self, d):
        print("copy_preview_create")
        d.swipe(0.7, 0.4, 0.7, 0.1, 0.13)
        d.xpath(copy_previous_btn_xpath).click()
        if d.xpath(sougou_input_title_xpath).exists:
            d.press("back")
        #创建时间-current
        d.xpath(to_set_start_date_xpath).click()
        d.swipe(0.869, 0.906, 0.864, 0.651, 0.5)
        if d.xpath(sougou_input_title_xpath).exists:
            d.press("back")
        d.xpath(ensure_start_date_xpath).click()
        time.sleep(1)
        d.swipe(0.7, 0.5, 0.7, 0.03, 0.1)
        d.xpath(save_create_btn_xpath).click()
        self.check_preview_create(d)
        d.press("back")
        return self
